package com.example.qx.adapter;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.qx.R;
import com.example.qx.lei.cos;
import com.example.qx.lei.leilianxr;
import com.example.qx.lianxra.liaotian;
import com.example.qx.lianxra.xinpenyou;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class lianxradapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<leilianxr> list ;
 private List<leilianxr> paihaolist;
private Map<Character,List<leilianxr>> groupdata;
private  Context context;
 public lianxradapter(Context context,List<leilianxr> list){
     this.context=context;
     this.list=list;
     paihaolist=new ArrayList<>();
       groupdata=new HashMap<>();
       for (int a=0;a<list.size();a++){
           if(!groupdata.containsKey(list.get(a).getSuoyin()))
                {
                    groupdata.put(list.get(a).getSuoyin(),new ArrayList<>());
                }
           groupdata.get(list.get(a).getSuoyin()).add(list.get(a));
       }
        List<Character> sortkey=new ArrayList<>(groupdata.keySet());
         Collections.sort(sortkey,(a,b)->a-b);
         paihaolist.add(new leilianxr("","新朋友",-1,'*',""));
         for(Character key:sortkey )
         {
             paihaolist.add(new leilianxr("",key.toString(),-10,' ',""));
             paihaolist.addAll(groupdata.get(key));
         }


 }
    @NonNull
    @Override
    public  RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
         View v;
        if(viewType==0) {
            v = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemlianxr, parent, false);
            return new lianxrHolder(v);
        }


        else {
             v = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemlianxr1, parent, false);
             return new suoyin(v);
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
           if(getItemViewType(position)==0){
         cos cos=new cos();
           leilianxr lianxr=paihaolist.get(position);
               ((lianxrHolder)holder).textView.setText(lianxr.getMingzi());


        if(!Objects.equals(lianxr.getTouxiang(), "s")&&lianxr.getTouxiang()!=null){

            String b=String.valueOf(lianxr.getId());
                      b=b+"tx";
                  File filee=context.getDir("youqishuju",context.MODE_PRIVATE);
                  String abc= filee.getAbsolutePath();
                  File filel=new File(abc+"/"+b);
                  if(!filel.exists())
                      cos.get(b,context);
                      Bitmap bitmap= BitmapFactory.decodeFile(abc+"/"+b);
            ((lianxrHolder)holder).imageView.setImageBitmap(bitmap);

                }
        else {
            ((lianxrHolder)holder).imageView.setVisibility(View.GONE);
        }
               ((lianxrHolder)holder).textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                     if(lianxr.getId()==-1)
                     {
                         Intent intent=new Intent(context, xinpenyou.class);
                         context.startActivity(intent);
                     }
                     else  {
                         Bundle bundle=new Bundle();
                         Intent intent=new Intent(context, liaotian.class);
                          bundle.putInt("zhanghao",lianxr.getId());
                          intent.putExtras(bundle);
                         context.startActivity(intent);
                     }

                }
            });}
           if(getItemViewType(position)==1) {
               ((suoyin)holder).textView.setText(paihaolist.get(position).getMingzi());
           }


    }
    @Override
    public int getItemCount() {return paihaolist.size();}

    @Override
    public int getItemViewType(int position) {
     int a=0;
     if(paihaolist.get(position).getId()==-10)
         a=1;
     return a;

 }
    public static class lianxrHolder extends RecyclerView.ViewHolder{
        private ImageView imageView;
        private TextView textView;

        public lianxrHolder(View v){
            super(v);
              imageView=v.findViewById(R.id.lianxrtouxiang);
              textView=v.findViewById(R.id.lianxrmingzi);

        }
    }
    public static class suoyin extends RecyclerView.ViewHolder{

     private TextView textView;

        public suoyin( View v) {
            super(v);
            textView=v.findViewById(R.id.suoyin);
        }
    }
}
